﻿using DLL;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.Oxytocin
{
    public partial class AddOxytocin : Form
    {
        #region 变量声明
        public NursingFormSystem.NurFormsService.NursingForm formService = new NursingFormSystem.NurFormsService.NursingForm();
        NursingFormRecord record = new NursingFormRecord();
        Tb_User LoginUser;   //员工信息
        string Id = string.Empty;
        PatientInfo Patientinfo;    //患者信息
        #endregion

        #region 构造函数
        public AddOxytocin(Tb_User loginUser, PatientInfo patientInfo, string id)
        {
            InitializeComponent();
            //成员变量
            this.LoginUser = loginUser;
            this.Patientinfo = patientInfo;
            this.Id = id;
            //绑定数据
            BindInfo(Id);
        }
        public AddOxytocin(string[] loginUser, string[] patientInfo)
        {
            InitializeComponent();
            //成员变量
            this.LoginUser = ArrayToObject.ArrayToLoginUser(loginUser);
            this.Patientinfo = ArrayToObject.ArrayToPatientInfo(patientInfo);
            this.Id = string.Empty;
            //绑定数据
            BindInfo(this.Id);
        }
        public AddOxytocin(string[] loginUser, string[] patientInfo, string id)
        {
            InitializeComponent();
            //成员变量
            this.LoginUser = ArrayToObject.ArrayToLoginUser(loginUser);
            this.Patientinfo = ArrayToObject.ArrayToPatientInfo(patientInfo);
            this.Id = id;
            //绑定数据
            BindInfo(this.Id);
        }
        #endregion

        #region 保存
        private void btn_Submit_Click(object sender, EventArgs e)
        {
            //日期判断
            if (string.IsNullOrEmpty(txtRiQi.Text.Trim()))
                txtRiQi.DateTime = DateTime.Now;
            //时间判断
            if (!txtShiJian.Text.Contains(":"))
            {
                txtShiJian.Focus();
                MessageBox.Show("时间需HH:mm格式，或使用双击显示当前时间");
                return;
            }

            txtShiJian.Text = DateTimeTran.TimeFillZero(txtShiJian.Text);
            if (string.IsNullOrEmpty(txtShiJian.Text)) txtShiJian.Text = DateTime.Now.ToString("HH:mm:ss");

            OXT oxt = new OXT();
            oxt.RiQi = txtRiQi.DateTime.ToString("yyyy-MM-dd");   //日期
            oxt.ShiJian = DateTimeTran.TimeFillZero(txtShiJian.Text);    //时间
            oxt.TaiCi = textTaiCi.Text;   //胎次
            oxt.ChanCi = textChanCi.Text;    //产次
            oxt.ZhenDuan = memoZhenDuan.Text;   //诊断
            oxt.CuiChanSu = comboBoxCuiChanSu.Text;    //催产素
            oxt.JiaCuiChanSu = textJiaCuiChanSu.Text;     //加催产素
            oxt.MeiFenZhongDiShu = textMeiFenZhongDiShu.Text;   //每分钟滴数
            oxt.GongSuoQiangDu = comGongKouQiangDu.Text;   //宫缩强度
            oxt.GongSuoChiXu = txtGongSuoChiXu.Text;   //宫缩持续
            oxt.GongSuoJianGe = txtGongSuoJianXie.Text;   //宫缩间隔
            oxt.GongKouWeiZhi = textGongKouWeiZhi.Text;      //宫口位置
            oxt.GongKouZhiDi = textGongKouZhiDi.Text;      //宫口质地
            oxt.GongKouDaXiao = textGongKouDaXiao.Text;     //宫口大小
            oxt.XianLuGaoDi = comboBoxXianLuGaoDi.Text;     //先露高低
            oxt.TaiMo = textTaiMo.Text;     //胎膜
            oxt.TaiXin = txtTaiXin.Text;   //胎心
            oxt.XueYa_Zuo = txtXueYaZuo.Text;   //血压_左
            oxt.XueYa_You = txtXueYaYou.Text;   //血压_右
            oxt.BeiZhu = memoBeiZhu.Text;  //备注
            if (string.IsNullOrEmpty(Id))   //签名
                oxt.QianMing = LoginUser.UserName;
            else
                oxt.QianMing = textQianMing.Text;
            oxt.FormId = (int)FormType.Oxytocins;   //表单Id
            oxt.FormName = EnumHelper.GetDescription(FormType.Oxytocins);   //表单名字
            record.FormId = (int)FormType.Oxytocins;   //表单Id
            record.FormName = EnumHelper.GetDescription(FormType.Oxytocins);   //表单名字
            record.PatientId = Patientinfo.PATIENT_ID;  //住院号
            record.VisitId = Patientinfo.VISIT_ID;   //住院次数
            record.PatientName = Patientinfo.PATIENT_NAME;   //患者姓名
            record.BedNo = Patientinfo.BED_NO;   //床号
            record.DepartmentId = Patientinfo.DEPARTMENT_ID;   //当前科室ID
            record.DepartmentName = Patientinfo.DEPARTMENT_NAME;   //当前科室名字
            record.AreaId = Patientinfo.NURSE_CELL_CODE;   //当前患者病区ID
            record.AreaName = Patientinfo.NURSE_CELL_NAME;   //当前患者病区名称
            record.AssessmentDate = Convert.ToDateTime(oxt.RiQi + " " + oxt.ShiJian);    //日期+时间
            record.Id = Id == "" ? 0 : Convert.ToInt32(Id);
            record.NursingFormData = XmlUtil.ObjectToXML(oxt, Encoding.UTF8);
            var ret = formService.SetNursingFormRecord(Newtonsoft.Json.JsonConvert.SerializeObject(record));
            if (ret.Code == 1)
            {
                MessageBox.Show(ret.M);
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show(ret.M);
            }
        }
        #endregion

        #region 显示赋值
        public void BindInfo(string Id)
        {
            memoZhenDuan.Text = Patientinfo.DIAGNOSE;    //诊断信息自动获取值
            textQianMing.Text = LoginUser.UserName;     //签名获取当前登录工号者
            txtRiQi.DateTime = DateTime.Now;     //默认显示当前的值
            txtShiJian.Text = DateTime.Now.ToString("HH:mm");
            if (string.IsNullOrEmpty(Id)) return;
            string sql = string.Format("SELECT TOP 1 NursingFormData FROM NursingFormRecord(NOLOCK) WHERE Id='{0}'", Id);
            DataTable tab = SqlHelper.ExecuteDataset(sql, System.Data.CommandType.Text).Tables[0];
            var NursingFormData = Convert.ToString(tab.Rows[0]["NursingFormData"]);
            OXT oxx = new OXT();
            oxx = XmlUtil.XMLToObject<OXT>(NursingFormData, Encoding.UTF8);
            if (!string.IsNullOrEmpty(oxx.RiQi))
                txtRiQi.DateTime = Convert.ToDateTime(oxx.RiQi);
            txtShiJian.Text = DateTimeHelper.HHmmssToHHmm(oxx.ShiJian);   //时间
            textTaiCi.Text = oxx.TaiCi;   //胎次
            textChanCi.Text = oxx.ChanCi;    //产次
            memoZhenDuan.Text = oxx.ZhenDuan;    //诊断
            comboBoxCuiChanSu.Text = oxx.CuiChanSu;    //催产素
            textJiaCuiChanSu.Text = oxx.JiaCuiChanSu;     //加催产素
            textMeiFenZhongDiShu.Text = oxx.MeiFenZhongDiShu;    //每分钟滴数
            comGongKouQiangDu.Text = oxx.GongSuoQiangDu;    //宫缩强度
            txtGongSuoChiXu.Text = oxx.GongSuoChiXu;     //宫缩持续
            txtGongSuoJianXie.Text = oxx.GongSuoJianGe;      //宫缩间隔
            textGongKouWeiZhi.Text = oxx.GongKouWeiZhi;     //宫口位置
            textGongKouZhiDi.Text = oxx.GongKouZhiDi;    //宫口质地
            textGongKouDaXiao.Text = oxx.GongKouDaXiao;    //宫口大小
            comboBoxXianLuGaoDi.Text = oxx.XianLuGaoDi;    //先露高低
            textTaiMo.Text = oxx.TaiMo;    //胎膜
            txtTaiXin.Text = oxx.TaiXin;    //胎心
            txtXueYaZuo.Text = oxx.XueYa_Zuo;   //血压_左
            txtXueYaYou.Text = oxx.XueYa_You;     //血压_右
            memoBeiZhu.Text = oxx.BeiZhu;     //备注
            textQianMing.Text = oxx.QianMing;     //签名

        }
        #endregion

        #region 取消
        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }
        #endregion


        #region 设置 enter键当做Tab切换
        /// <summary>
        /// 设置 enter键当做Tab切换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void TemperatureEdit1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                SendKeys.SendWait("{Tab}");
            }
        }
        #endregion

        #region 双击显示当前时间
        private void txtShiJian_DoubleClick_1(object sender, EventArgs e)
        {
            txtShiJian.Text = DateTime.Now.ToString("HH:mm");
        }
        #endregion

    }
}
